package Java网络爬虫;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author tyy
 * @Description TODO
 * @Since 2021/11/27
 */
public class UrlPool {
    public static void main(String[] args) {
        getUrl("https://www.nipic.com/");
    }

    private static void getUrl(String s) {
        Map<String, Boolean> oldMap = new LinkedHashMap<>();
        String oldLinkHost = "";
        Pattern p = Pattern.compile("(https?://)?[^/\\s]*");
        Matcher m = p.matcher(s);
        if (m.find()) {
            oldLinkHost = m.group();
        }
        oldMap.put(s, false);
        oldMap = crawlLinks(oldLinkHost, oldMap);
        for (Map.Entry<String, Boolean> mapping : oldMap.entrySet()) {
            System.out.println("链接 = " + mapping.getKey());
        }

    }

    private static Map<String, Boolean> crawlLinks(String oldLinkHost, Map<String, Boolean> oldMap){
        Map<String, Boolean> newMap = new LinkedHashMap<>();
        String oldLink = "";
        //region Description
        for (Map.Entry<String, Boolean> mapping : oldMap.entrySet()) {
            System.out.println("链接 = " + mapping.getKey() + "=======" + mapping.getValue());
            if (!mapping.getValue()) {
            }
            //region Description
            oldLink = mapping.getKey();
            //endregion
            try {
                URL url = new URL(oldLink);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                if (conn.getResponseCode() == 200) {
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(conn.getInputStream())
                    );
                    Pattern p = Pattern.compile("<a.*?href=[\"']?((https?://)?/?[^\"']+)[\"']?.*?>(.+)</a>");
                    Matcher matcher = null;
                    String line = "";
                    while ((line = reader.readLine()) != null){
                        matcher = p.matcher(line);
                        if (matcher.find()) {
                            String newLink = matcher.group(1).trim();
                            if (!newLink.startsWith("http")) {
                                if (newLink.startsWith("/")) {
                                    newLink = oldLinkHost + newLink;

                                }else{
                                    newLink = oldLinkHost + "/"+ newLink;
                                }
                            }
                            if (newLink.endsWith("/")) {
                                newLink = newLink.substring(0, newLink.length() - 1);
                            }
                            if (!oldMap.containsKey(newLink)
                                    && !newMap.containsKey(newLink)
                                    && newLink.startsWith(oldLinkHost)
                                    ) {
                                newMap.put(newLink, false);
                            }
                        }
                    }

                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            oldMap.replace(oldLink, false, true);

        }
        //endregion
        if (!newMap.isEmpty()) {
            oldMap.putAll(newMap);
            oldMap.putAll(crawlLinks(oldLinkHost, oldMap));

        }
        return oldMap;

    }
}
